package mhys.View.Admin;
import mhys.Dao.LoginDao;
import mhys.Entity.User;
import mhys.View.Teacher.App;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import static java.awt.Toolkit.getDefaultToolkit;

public class Login {
    Image icon = getDefaultToolkit().getImage("D:/download/warriors.png");
    LoginDao loginDao = new LoginDao();
    public Login() {
        JFrame jFrame = new JFrame();
        jFrame.setTitle("用户登录");
        jFrame.setSize(400, 250);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(3);
        jFrame.setLayout(null);
        jFrame.setIconImage(icon);

        JLabel title = new JLabel("管理员登录");
        Font font = new Font("黑体",Font.PLAIN,22);
        title.setFont(font);
        title.setBounds(150, 10, 200, 30);

        JLabel userLabel = new JLabel("工号：");
        userLabel.setBounds(50, 60, 50, 30);

        JTextField userField = new JTextField();
        userField.setBounds(110, 60, 200, 30);

        JLabel pwdLabel = new JLabel("密码：");
        pwdLabel.setBounds(50, 100, 50, 30);

        JPasswordField pwdField = new JPasswordField();
        pwdField.setBounds(110, 100, 200, 30);

        JButton loginButton = new JButton("登录");
        loginButton.setBounds(160, 150, 90, 30);

        userField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (userField.getText().equals("请输入工号")){
                    userField.setText("");     //将提示文字清空
                    userField.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (userField.getText().equals("")){
                    userField.setForeground(Color.gray); //将提示文字设置为灰色
                    userField.setText("请输入工号");     //显示提示文字
                }
            }
        });

        loginButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userField.getText();
                String password = pwdField.getText();
                User user = loginDao.getUser(username);
                if (user == null) {
                    JOptionPane.showMessageDialog(jFrame,"账号不存在");
                }else {
                    if (password.equals(user.getPassword())){
                        JOptionPane.showMessageDialog(jFrame,"登录成功");
                        new App();
                        jFrame.dispose();
                    }else {
                        JOptionPane.showMessageDialog(jFrame,"密码错误");
                    }
                }
            }
        });

        jFrame.add(title);
        jFrame.add(userLabel);
        jFrame.add(userField);
        jFrame.add(pwdLabel);
        jFrame.add(pwdField);
        jFrame.add(loginButton);
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new Login();
    }
}
